<template>
  <div class="goodsinfo-container">
    <!-- 购物车小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 轮播图区域 -->

    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <!-- 这是一个最简单的卡片视图控件；卡片视图常用来显示完整独立的一
          段信息，比如一篇文章的预览图、作者信息、点赞数量等-->
          <swiper :BannerList="list" :isfull="false"></swiper>
        </div>
      </div>
    </div>
    <!-- 购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.name}}</div>
      <div class="mui-card-content">
        <p class="price">
          市场价:
          <del>￥{{goodsInfo.old}}</del>&nbsp;&nbsp;&nbsp;
          销售价:
          <span class="now_price">￥{{goodsInfo.now}}</span>
        </p>
        <p>
          购买数量
          <numbox @getcount ="getSelectCount"
          :max="goodsInfo.have"></numbox>
        </p>
        <p>
          <mt-button type="primary" size="small">立即购买</mt-button>
          <mt-button type="danger" size="small" @click="addToShopCart">加入购物车</mt-button>
          <!-- 如何拿到购物车数字 -->
          <!-- 数字在子组件   子组件给父组件：父组件给子组件调用方法 子组件返回 -->
        </p>
      </div>
    </div>
    <!-- 参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.canshu}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>货号:{{goodsInfo.huohao}}</p>
          <p>库存:{{goodsInfo.have}}</p>
          <p>上架日期:{{goodsInfo.add_time }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" @click="gotoDesc()">图文介绍</mt-button>
        <mt-button type="danger" size="large" @click="goComments()">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import "../../../aaa_lianxi/mock_goods_list";
import swiper from "../subcomponents/swiper.vue";
import numbox from "../subcomponents/GoodsInfo_NumberBox.vue";
import mui from "../../lib/mui.min.js";
export default {
  data() {
    return {
      list: [],
      goodsInfo: {},
      ballFlag: false //小球,
      ,selectCount:1
    };
  },
  components: { swiper, numbox },
  created() {
    this.getBannerList();
  },

  methods: {
    getBannerList() {
      this.$http.get("/home/goodslist").then(res => {
        // console.log(res);
        this.goodsInfo = res.body.goods[0];
        //循环轮播图
        res.body.goods.forEach(item => {
          item.img = item.src;
        });
        this.list = res.body.goods;
      });
    },
    // 跳转详情页
    gotoDesc() {
      this.$router.push({ name: "goodsdesc", param: { id: 1 } });
    },
    // 跳转评论
    goComments() {
      this.$router.push({ name: "goodscomment" });
    },
    addToShopCart() {
      this.ballFlag = !this.ballFlag;
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;

      //小球唯一分辨率 写死了  换一机型就拉稀
      //动态写translatione的坐标   getBoundingclientrect()
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // const badgePosition= this.$parent.badge.getBoundingClientRect();
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 0.3s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectCount(count){
      this.selectCount=count;
      console.log('父组件拿到的值：'+this.selectCount);
      
    }
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  margin: 0;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
}
.mui-card-footer {
  display: block;
  button {
    margin: 10px 0;
  }
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  z-index: 99;
  left: 123px;
  top: 376px;
}
</style>
